package com.ecoolex.hotel.saas.platform.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.AddDepartmentDTO;
import com.ecoolex.hotel.saas.pojo.EditDepartmentDTO;
import com.ecoolex.hotel.saas.ws.SalesUserService;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "部门")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = DepartmentGraphql.AUTH,
	name = "部门管理",
	remark = "部门管理")
public class DepartmentGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-DEPARTMENT";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@Reference
	private StoreService service;
	
	@Reference
	private SalesUserService salesUserService;
	
	@TypeField(type = Department.class, field = "salesUserNumber", description = "关联的销售员数量")
	public Long salesUserNumber(@Source Department source) {
		return salesUserService.countByDepartmentId(source.getDepartmentId());
	}
	
	@QueryField(field = "department", description = { "部门详情" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取部门详情")
	public Department get(@NonNull Integer departmentId, @NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getDepartment(departmentId, storeId);
	}
	
	@QueryField(field = "departmentList", description = { "部门列表" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "获取部门列表")
	public Pagination<Department> query(String name,@NonNull Integer storeId, @NonNull Page page, PlatformMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return service.queryDepartment(name, storeId, page);
	}
	
	@MutationField(field = "addDepartment", description = "添加部门")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加部门")
	public Department add(@NonNull AddDepartmentDTO add, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		Store store = service.getStore(add.getStoreId());
		Assert.isTrue(Check.notNull(store),ResultCode.VERIFY.build(1), "商户Id={}不存在", add.getStoreId());
		return service.addDepartment(add, user.userName());
	}
	
	@MutationField(field = "editDepartment", description = "编辑部门")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑部门")
	public Department edit(@NonNull EditDepartmentDTO edit, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		Department old = service.getDepartment(edit.getDepartmentId(), edit.getStoreId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "部门不存在");
		old = BeanUtil.copy(edit, old);
		return service.editDepartment(old, user.userName());
	}
	
	@MutationField(field = "delDepartment", description = "删除部门")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除部门")
	public Boolean del(@NonNull Integer departmentId,@NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		Department old = service.getDepartment(departmentId, storeId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "部门不存在");
		return service.delDepartment(old, user.userName());
	}
}
